package com.xt.partjobback.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xt.partjobback.entity.User;
import com.xt.partjobback.exception.OperateException;
import com.xt.partjobback.exception.PermissionsException;
import com.xt.partjobback.mapper.UserMapper;
import com.xt.partjobback.service.UserService;
import com.xt.partjobback.utils.TokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.Date;

/**
 * @author leotong
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    private final UserMapper userMapper;

    @Autowired
    public UserServiceImpl(UserMapper userMapper) {
        this.userMapper = userMapper;
    }

    @Override
    public String userLogin(User user) {
        Assert.notNull(user,"用户账号密码不允许为空");
        Assert.notNull(user.getAccount(),"用户账号不允许为空");
        Assert.notNull(user.getPassword(),"用户密码不允许为空");
        QueryWrapper<User> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("account",user.getAccount());
        queryWrapper.eq("role",user.getRole());
        User result = this.getOne(queryWrapper);
        Assert.notNull(result,"当前登陆用户不存在");
        if (!result.getPassword().equals(user.getPassword())){
            throw new PermissionsException("密码输入错误");
        }
        return result.getRole()? TokenUtil.createToken(String.format("admin:%s",1)):TokenUtil.createToken(String.format("user:%s",result.getId()));
    }

    @Override
    public String userRegister(User user) {
        Assert.notNull(user,"用户注册信息不允许为空");
        Assert.notNull(user.getAccount(),"用户账号不允许为空");
        Assert.notNull(user.getPassword(),"用户密码不允许为空");
        QueryWrapper<User> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("account",user.getAccount());
        User result = this.getOne(queryWrapper);
        if (result!=null){
            throw new OperateException("当前账号已经被使用了，请重新输入");
        }
        user.setCreateTime(new Date());
        user.setName("默认姓名");
        this.save(user);
        return TokenUtil.createToken(String.format("user:%s",user.getId()));
    }

    @Override
    public boolean updateUser(User user) {
        Assert.notNull(user,"用户更新信息不能为空");
        Assert.notNull(user.getId(),"用户ID不能为空");
        return this.updateById(user);
    }

    @Override
    public boolean deleteUser(Integer id) {
        Assert.notNull(id,"用户ID不能为空");
        if (id==1){
            throw new PermissionsException("当前id错误，禁止删除");
        }
        return this.removeById(id);
    }

    @Override
    public Page<User> getUserByCondition(User user) {
        Page<User> page=new Page<>(user.pageNum==null?1: user.getPageNum(),user.pageSize==null?5: user.getPageSize());
        QueryWrapper<User> queryWrapper=new QueryWrapper<>();
        if (user.getName()!=null){
            queryWrapper.like("name",user.getName());
        }
        if (user.getRole()!=null){
            queryWrapper.eq("role",user.getRole());
        }
        return userMapper.selectPage(page, queryWrapper);
    }

}
